package com.v2.k27;

import com.v2.k15.D;
import com.v2.k25.TransactionAnnotationService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

@Component
public class TransactionManagerService {

	private PlatformTransactionManager platformTransactionManager;
	private JdbcTemplate jdbcTemplate;

	public TransactionManagerService(PlatformTransactionManager platformTransactionManager, DataSource dataSource) {
		this.platformTransactionManager = platformTransactionManager;
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public void test() {
		DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
		defaultTransactionDefinition.setName("kzxTrans1");

		TransactionStatus status = platformTransactionManager.getTransaction(defaultTransactionDefinition);
		try {
			jdbcTemplate.execute("insert into order_item_his(order_item_id, his_shard) values(132,'2021-06-06');");
//			int i = 1/0;
		} catch (Exception e) {
			platformTransactionManager.rollback(status);
			throw e;
		}
		platformTransactionManager.commit(status);

	}



}
